package com.zyht.website.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.wx.util.JsonUtil;
import com.wx.util.StringUtils;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.util.FileUploadUtil;
import com.zyht.common.util.Page;
import com.zyht.website.entity.IndexBanner;
import com.zyht.website.entity.IndexBannerExample;
import com.zyht.website.service.BannerService;

/**
 * <b>描述：</b>	banner<br/>
 * <b>作者：</b>Bob <br/>
 * <b>修改日期：</b>2016年8月23日 - 下午2:11:59<br/>
 *
 */
@Controller
@RequestMapping("admin/banner")
public class BannerManageController extends AbsManageBaseController
{
	@Resource
	private BannerService bannerService;
	
	private final String VIEW_PREFIX = "website/banner/";
	
	@RequestMapping("/index")
	public String index(Integer currentPage, Model model,HttpServletRequest request){
		IndexBannerExample kbe = new IndexBannerExample();
		kbe.setOrderByClause("create_time DESC");
		if (null == currentPage) {
			currentPage = 1;
		}
		int pageSize = getPageSize();
		PageHelper.startPage(currentPage, pageSize);
		com.github.pagehelper.Page<IndexBanner> kgBanners = (com.github.pagehelper.Page<IndexBanner>) bannerService.list(kbe);
		for(IndexBanner banner : kgBanners){
		    banner.setImgUrl(FileUploadUtil.getImageThumbPath(banner.getImgUrl()));
		}
		Page<IndexBanner> page = new Page<IndexBanner>((int)kgBanners.getTotal(), currentPage, pageSize);
		page.setRecordList(kgBanners.getResult());
		model.addAttribute("page", page);
		return makeViewName("index");
	}
	
	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年8月23日 - 下午2:12:10<br/>
	 * <b>功能说明：</b>	添加页面<br/>
	 * @param bid
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("toadd")
	public String toadd(Integer bid, Model model, HttpServletRequest request) {
		//优先级
		int sort = 0;
		IndexBannerExample kbe = new IndexBannerExample();
		kbe.setOrderByClause("sort Desc");
		PageHelper.startPage(1, 1);
		List<IndexBanner> sList = bannerService.list(kbe);
		if(sList.size() > 0){
			sort = sList.get(0).getSort();
		}
		model.addAttribute("sort", sort);
		//修改
		IndexBanner zzyhBanner = null;
		if(bid!=null){
			zzyhBanner = bannerService.findOneById(bid);
		}
		model.addAttribute("bean", zzyhBanner);
		return makeViewName("add");
	}
	

	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年8月23日 - 下午2:12:27<br/>
	 * <b>功能说明：</b>	添加<br/>
	 * @param model
	 * @param request
	 * @param id
	 * @param name
	 * @param title
	 * @param href
	 * @param imgUrl
	 * @param sort
	 * @param isValid
	 * @param typeValid
	 * @param dueDate
	 * @return
	 */
	@RequestMapping("persis")
	public String persis( Model model,HttpServletRequest request,
			@RequestParam(value="id",required=false)Integer id,
			@RequestParam(value="name",required=false)String name,
			@RequestParam(value="title")String title,
			@RequestParam(value="href")String href,
			@RequestParam(value="imgUrl")String imgUrl,
			@RequestParam(value="sort")Integer sort,
			@RequestParam(value="isValid")Integer isValid,
			@RequestParam(value="typeValid")Integer typeValid,
			@RequestParam(value="dueDate")Date dueDate) {
		IndexBanner bean = new IndexBanner();
		bean.setName(name);
		bean.setTitle(title);
		bean.setHref(href);
		bean.setImgUrl(imgUrl);
		bean.setSort(sort);
		bean.setIsValid(isValid);
		bean.setTypeValid(typeValid);
		bean.setDueDate(dueDate);
		if(id==null){
			bean.setCreateTime(new Date());
			bannerService.addOne(bean);
		}else{
			bean.setId(id);
			bannerService.updateById(bean);
		}
		request.setAttribute("msg", "保存成功");
		request.setAttribute("redirect", "admin/banner/index");
		return "message";
	}
	
	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年8月23日 - 下午2:12:40<br/>
	 * <b>功能说明：</b>	删除<br/>
	 * @param id
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("del")
	public String del(Integer id, Model model, HttpServletRequest request) {
		    if(null != id){
		    	IndexBanner entity = bannerService.findOneById(id.intValue());
			    int  row = bannerService.deleteById(id);
			   //删除图片等资源
			    if(row > 0){
					try {
						String imgPath = entity.getImgUrl();
						String rootPath = request.getSession().getServletContext().getRealPath("/");
					    String realPath = rootPath+imgPath;
					    File file = new File(realPath);
					    if(file.exists()){
					    	file.delete();
					    }
					} catch (Exception e) {
						logger.debug("删除图片异常"+e.getMessage());
					}
			    }
		    }
		    model.addAttribute("msg", "删除成功");
			model.addAttribute("redirect", "admin/banner/index");
			return "message";
	}

	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年8月23日 - 下午2:12:49<br/>
	 * <b>功能说明：</b>	查找某一个<br/>
	 * @param query
	 * @param currentPage
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("search")
	public String del(String query,Integer currentPage, Model model,HttpServletRequest request) {
		IndexBannerExample kbe = new IndexBannerExample();
		if(!StringUtils.isEmpty(query)){
			kbe.createCriteria().andTitleLike("%"+query+"%");
		}
		if (null == currentPage) {
			currentPage = 1;
		}
		int pageSize = getPageSize();
		PageHelper.startPage(currentPage, pageSize);
		List<IndexBanner> gynsBanners = bannerService.list(kbe);
		Page<IndexBanner> page = new Page<IndexBanner>(gynsBanners.size(), currentPage, pageSize);
		page.setRecordList(gynsBanners);
		model.addAttribute("page", page);
		model.addAttribute("query", query);
		return makeViewName("index");
	}
	
	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年8月23日 - 下午2:11:32<br/>
	 * <b>功能说明：</b>	<br/>
	 * @param id
	 * @param isValid
	 * @return
	 */
	@RequestMapping("setValid")
	public @ResponseBody String ajaxSetValid(Integer id,Integer isValid) {
		if(id==null){id=0;}
		if(isValid==null){isValid=0;}
		Map<String,Object> map  = new HashMap<String, Object>();
		IndexBanner banner = new IndexBanner();
		banner.setId(id);
		banner.setIsValid(isValid);
		int num = bannerService.updateById(banner);
		if(num > 0){
			map.put("success",true);
		}else{
			map.put("success",false);
		}
		return JsonUtil.Object2Json(map);
	}
	
	//拼装视图
	private String makeViewName(String page){
		return VIEW_PREFIX.concat(page);
	}
}
